<?php

namespace app\app\controller;

use app\app\model\UserMsgModel;
use think\db\exception\DbException;
use think\facade\Db;

class UserMsg extends BaseCurd
{
    /**
     * initialize
     * @return void
     */
    public function initialize()
    {
        $this->model = UserMsgModel::class;
    }

    /**
     * C&U
     * @return void
     */
    public function save()
    {
        $id = input('id', '');
        $param = $this->request->param();

        if (empty($id)) {
            $model = new $this->model();
//            $model->id = RandModel::randId();
        } else {
            $model = $this->model::where('id', $id);
        }

        $model->save($param);

        $this->success('success');
    }

    /**
     * R
     * @return void
     * @throws DbException
     */
    public function list()
    {
        $push = 1;

        $uid = input('uid', '');
        empty($uid) && $this->error('请传入uid');
        $where[] = ['uid', '=', $uid];

        $total_unread = $this->model::where('uid', $uid)
            ->whereIn('is_read', [0 ,2])
            ->count();
        $total = $this->model::where('uid', $uid)
            ->count();

        // 已读筛选
        $select = input('select_is_read/d', 0);
        switch ($select) {
            case 0:
                $where[] = ['is_read', '=', 0];
                break;
            case 1:
                $where[] = ['is_read', 'in', [1, 2]];
                break;
            case 2:
            default:
        }

        $page_size = input('pageSize', '20');

//        $tmp = [];
//        foreach ($where as $k => $v) {
//            $tmp['a.' . $k] = $v;
//        }
//        $where = $tmp;
//        unset($tmp);

        $data = $this->model::where($where)->order('is_read', 'desc')->paginate($page_size);

        empty($data) && $this->error('查询为空', ['total_unread' => $total_unread, 'total_user' => $total, 'push' => $push]);
        $data = $data->toArray();
        empty($data['data']) && $this->error('未找到符合条件的数据', ['total_unread' => $total_unread, 'total_user' => $total, 'push' => $push]);

        $data['total_unread'] = $total_unread;
        $data['total_user'] = $total;
        $data['push'] = $push;
//        $this->model::where($where)->update(['is_read' => 1]);

        usort($data['data'], [$this, 'compare']);

        $this->success('success', $data);
    }

    /**
     * @param $a
     * @param $b
     * @return int
     */
    private function compare($a, $b)
    {
        $sort_array = [0, 2, 1];
        return (array_search($a['is_read'], $sort_array) < array_search($b['is_read'], $sort_array)) ? -1 : 1;
    }

    /**
     * detail
     * @param $id
     * @return void
     */
    public function detail($id = 0)
    {
        !$id && $this->error('请传入id');

        $data = $this->model::where('id', $id)->find();
        empty($data) && $this->error('查询为空');

        $this->success('success', $data);
    }

    /**
     * D
     * @return void
     * @throws DbException
     */
    public function del($id = 0)
    {
        !$id && $this->error('请传入id');

        $this->model::where('id', $id)->delete();

        $this->success('success');
    }

    /**
     * 已读
     * @return void
     */
    public function read()
    {
        $uid = input('uid', '');
        empty($uid) && $this->error('请传入uid');

        $id = input('id', '');
        empty($id) && $this->error('请传入id');

        $status = input('status', 0);
        $status = intval($status);
        !(in_array($status, [0, 1, 2])) && $this->error('请传入正确的状态');

        $this->model::whereIn('id', $id)->update(['is_read' => $status]);

        if ($status == 1) $this->model::whereIn('id', $id)->update(['read_time' => date('Y-m-d H:i:s')]);

        $total = $this->model::where('uid', $uid)->whereIn('is_read', [0, 2])->count();

        $this->success('success', ['total' => $total]);
    }

    /**
     * 推送设备
     * @return void
     * @throws DbException
     */
    public function reg()
    {
        $uid = input('uid', '');
        empty($uid) && $this->error('请传入uid');

        $app_client_id = input('app_client_id', '');
        empty($app_client_id) && $this->error('请传入app_client_id');

        $app_os = input('app_os', '');
        empty($app_os) && $this->error('请传入app_os');
        !(in_array($app_os, ['android', 'ios'])) && $this->error('请传入正确的系统');

        Db::table('sys_user')->where('user_id', $uid)->update([
            'app_client_id' => $app_client_id,
            'app_os' => $app_os,
        ]);

        $this->success('success');
    }
}